<?php

namespace app\admin\controller;

use think\Db;

use app\admin\model\User as UserModel;
use think\Request;

class User extends Admin
{
    public $umodel;

    // 初始化
    public function _initialize()
    {
        parent::_initialize();
        $this->umodel = new UserModel;
    }

    public function index()
    {
        $data = new UserModel;
        // var_dump($data);die;
        $list = $data->index();
        
        return view('user/index',['list' => $list]);

    }

    public function add()
    {
        return view('user/add');
    }

    public function doAdd(Request $request)
    {
        $data = input('post.');
        $res = new UserModel;
        $list = $res->doAdd($data);
        if ($list > 0) {
            $this->success("添加成功！", Url('User/index'));
        } else {
            $this->error("添加失败！");
        }
    }

    public function delete($id)
    {
        $res = new UserModel;
        $list = $res->doDelete($id);
         if ($list > 0) {
            // $this->success("删除成功！", Url('User/index'));
            // return 返回一个json数据(ajax)
            return json(['status'=>'success']);
        } else {
            return json(['status'=>'success']);
            
        }
    }

    public function edit($id)
    {
        $list = Db::table('lp_user')
            ->field('name,id,tel,status')
            ->find($id);
//        dump($list);die;
        return view('user/edit', [
            'list' => $list
        ]);
    }

    public function doEdit(Request $request,$id)
    {

        // $data = input('post.');
        // $res = new UserModel;
        // $list = $res->doEdit($data);
        
        // if ($list > 0) {
        //     $info['status'] = true;
        //     $info['msg']='success';

        // } else {
        //     $this->error("更新失败！");
        // }
        // return json($info);
        
       // var_dump($request);die;

        $info = $request->put();
        // 处理数据
        $data = [
            'id' => $info['id'],
            'name' => $info['name'],
            'tel' => $info['tel'],
            'pwd' => $info['pwd']
        ];
        $res = new UserModel;
        $list = $res->doEdit($data);

        if ($list > 0) {
             $info['status'] = true;
             $info['msg']='success';
        } else {
             $info['status'] = false;
             $info['msg']='success';
        }

        return json($info);

    }

}